/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package canvas;

import java.io.IOException;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.midlet.MIDletStateChangeException;
import midlet.MidletPlay;

/**
 * @author LTTDNQ
 */
public class MIDPCanvasMenu extends GameCanvas {

    private MidletPlay _midlet;
    private Graphics _graphics;
    private int _chieudai = getWidth();
    private int _chieucao = getHeight();
    private int imgWidth, imgHeight;
    Image imgBg, imgNew, imgOpt, imgHow, imgHigh, imgExit;

    /**
     * constructor
     */
    public MIDPCanvasMenu(MidletPlay midlet) {
        super(false);
        this._midlet = midlet;
        _midlet.setHeightMobile(_chieucao);
        _midlet.setWidthMobile(_chieudai);
        _graphics = this.getGraphics();
        setFullScreenMode(true);
        this._graphics = this.getGraphics();
        try {
            imgBg = Image.createImage("/Images/imgBackground.png");

            imgNew = Image.createImage("/Images/imgNewGame.png");
            imgOpt = Image.createImage("/Images/imgOptions.png");
            imgHow = Image.createImage("/Images/imgHowtoPlay.png");
            imgHigh = Image.createImage("/Images/imgHighScore.png");
            imgExit = Image.createImage("/Images/imgExit.png");
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    /**
     * paint
     */
    public void paintMenu() {

        imgWidth = imgExit.getWidth();
        imgHeight = imgExit.getHeight();
        _graphics.drawImage(imgBg, 0, 0, Graphics.LEFT | Graphics.TOP);
        _graphics.drawImage(imgNew, _chieudai * 5 / 10, _chieucao * 4 / 10, Graphics.HCENTER | Graphics.VCENTER);
        _graphics.drawImage(imgOpt, _chieudai * 5 / 10, _chieucao * 5 / 10, Graphics.HCENTER | Graphics.VCENTER);
        _graphics.drawImage(imgHow, _chieudai * 5 / 10, _chieucao * 6 / 10, Graphics.HCENTER | Graphics.VCENTER);
        _graphics.drawImage(imgHigh, _chieudai * 5 / 10, _chieucao * 7 / 10, Graphics.HCENTER | Graphics.VCENTER);
        _graphics.drawImage(imgExit, _chieudai * 5 / 10, _chieucao * 8 / 10, Graphics.HCENTER | Graphics.VCENTER);


    }

    private void clear_Item(int x) {

        try {
            if (x == 1) {
                imgNew = Image.createImage("/Images/imgNewGame.png");
                _graphics.drawImage(imgNew, _chieudai * 5 / 10, _chieucao * 4 / 10, Graphics.HCENTER | Graphics.VCENTER);
            } else if (x == 2) {
                imgOpt = Image.createImage("/Images/imgOptions.png");
                _graphics.drawImage(imgOpt, _chieudai * 5 / 10, _chieucao * 5 / 10, Graphics.HCENTER | Graphics.VCENTER);
            } else if (x == 3) {
                imgHow = Image.createImage("/Images/imgHowtoPlay.png");
                _graphics.drawImage(imgHow, _chieudai * 5 / 10, _chieucao * 6 / 10, Graphics.HCENTER | Graphics.VCENTER);
            } else if (x == 4) {
                imgHigh = Image.createImage("/Images/imgHighScore.png");
                _graphics.drawImage(imgHigh, _chieudai * 5 / 10, _chieucao * 7 / 10, Graphics.HCENTER | Graphics.VCENTER);
            } else {
                imgExit = Image.createImage("/Images/imgExit.png");
                _graphics.drawImage(imgExit, _chieudai * 5 / 10, _chieucao * 8 / 10, Graphics.HCENTER | Graphics.VCENTER);
            }
            flushGraphics();
        } catch (IOException ex) {
            ex.printStackTrace();
        }

    }

    private void Draw_nextItem(int x) {
        try {
            if (x == 1) {
                imgNew = Image.createImage("/Images/imgNewGamePress.png");
                _graphics.drawImage(imgNew, _chieudai * 5 / 10, _chieucao * 4 / 10, Graphics.HCENTER | Graphics.VCENTER);
            } else if (x == 2) {
                imgOpt = Image.createImage("/Images/imgOptionsPress.png");
                _graphics.drawImage(imgOpt, _chieudai * 5 / 10, _chieucao * 5 / 10, Graphics.HCENTER | Graphics.VCENTER);
            } else if (x == 3) {
                imgHow = Image.createImage("/Images/imgHowtoPlayPress.png");
                _graphics.drawImage(imgHow, _chieudai * 5 / 10, _chieucao * 6 / 10, Graphics.HCENTER | Graphics.VCENTER);
            } else if (x == 4) {
                imgHigh = Image.createImage("/Images/imgHighScorePress.png");
                _graphics.drawImage(imgHigh, _chieudai * 5 / 10, _chieucao * 7 / 10, Graphics.HCENTER | Graphics.VCENTER);
            } else if (x == 5) {
                imgExit = Image.createImage("/Images/imgExitPress.png");
                _graphics.drawImage(imgExit, _chieudai * 5 / 10, _chieucao * 8 / 10, Graphics.HCENTER | Graphics.VCENTER);
            }
            flushGraphics();
        } catch (IOException ex) {
            ex.printStackTrace();
        }

    }

    /**
     * Called when a key is pressed.
     */
//Sự kiện cho lựa chọn bằng nhập từ bàn phím
    protected void keyPressed(int keyCode) {
        
        int _viTri = _midlet.getViTriMenu();
        int action = getGameAction(keyCode);
        System.out.println(_viTri);
        switch (action) {
            case 8:
                if (_viTri == 1) {
                    _midlet.vaoManHinhChoi();
                } else if (_viTri == 2) {
                    _midlet.vaoManHinhOptions();
                } else if (_viTri == 3) {
                    _midlet.vaoManHinhHowToPlay();
                } else if (_viTri == 4) {
                    _midlet.vaoManHinhHighScore();
                } else if (_viTri == 5) {
                    try {
                        _midlet.exit(0);
                    } catch (MIDletStateChangeException ex) {
                        ex.printStackTrace();
                    }

                }
            case DOWN:
                
                clear_Item(_viTri);
                _viTri = _viTri + 1;
                if (_viTri > 5) {
                    _viTri = 1;
                }
                
                _midlet.setViTriMenu(_viTri);
                _viTri = _midlet.getViTriMenu();
                Draw_nextItem(_viTri);
                break;
            case UP:
                
                clear_Item(_viTri);
                _viTri = _viTri-1;
                if (_viTri < 1) {
                    _viTri = 5;
                } 
                _midlet.setViTriMenu(_viTri);
                _viTri = _midlet.getViTriMenu();
                Draw_nextItem(_viTri);
                break;
        }

    }

    /**
     * Called when a key is released.
     */
    protected void keyReleased(int keyCode) {
    }

    /**
     * Called when a key is repeated (held down).
     */
    protected void keyRepeated(int keyCode) {
    }

    /**
     * Called when the pointer is dragged.
     */
    protected void pointerDragged(int x, int y) {
    }

    /**
     * Called when the pointer is pressed.
     */
    //Sự kiện cho menu khi trỏ chuột
    protected void pointerPressed(int x, int y) {
        //Click vào chơi game
        if (x > ((_chieudai * 5) / 10 - imgWidth / 2) && x < ((_chieudai * 5) / 10 + imgWidth / 2) && y > ((_chieucao * 4) / 10 - imgHeight / 2) && y < ((_chieucao * 4) / 10) + imgHeight / 2) {
            _midlet.vaoManHinhChoi();
        } //Click vào tùy chọn
        else if (x > ((_chieudai * 5) / 10 - imgWidth / 2) && x < ((_chieudai * 5) / 10 + imgWidth / 2) && y > ((_chieucao * 5) / 10 - imgHeight / 2) && y < ((_chieucao * 5) / 10) + imgHeight / 2) {
            _midlet.vaoManHinhOptions();
        } //Click vào Hướng dẫn chơi
        else if (x > ((_chieudai * 5) / 10 - imgWidth / 2) && x < ((_chieudai * 5) / 10 + imgWidth / 2) && y > ((_chieucao * 6) / 10 - imgHeight / 2) && y < ((_chieucao * 6) / 10) + imgHeight / 2) {
            _midlet.vaoManHinhHowToPlay();
        } //Click vào điểm 
        else if (x > ((_chieudai * 5) / 10 - imgWidth / 2) && x < ((_chieudai * 5) / 10 + imgWidth / 2) && y > ((_chieucao * 7) / 10 - imgHeight / 2) && y < ((_chieucao * 7) / 10) + imgHeight / 2) {
            _midlet.vaoManHinhHighScore();
        } //Click vào thoát
        else if (x > ((_chieudai * 5) / 10 - imgWidth / 2) && x < ((_chieudai * 5) / 10 + imgWidth / 2) && y > ((_chieucao * 8) / 10 - imgHeight / 2) && y < ((_chieucao * 8) / 10) + imgHeight / 2) {
            try {
                _midlet.exit(0);
            } catch (MIDletStateChangeException ex) {
                ex.printStackTrace();
            }
        }
    }

    /**
     * Called when the pointer is released.
     */
    protected void pointerReleased(int x, int y) {
    }
    /**
     * Called when action should be handled
     */
}
